/*
 *  $Id: GameCardDeck.cs 999 2010-04-28 13:12:07Z thenn.erannor $
 *	This file is a part of Ragima CCG.
 *	(C) 2008-2009 Ragima development group.
 *
 *	Licensed under the terms of the GNU GPL License version 2.
 *
 */

using System.Collections.Generic;

namespace Ragima {
    public class GameCardDeck {
        private List<Card> _cards;

        public GameCardDeck() {
            _cards = new List<Card>();
        }
        public GameCardDeck(CardDeck deck): this() {
            Add(deck);
        }

        public int Count {
            get { return _cards.Count; }
        }

        public void Add(Card card) {
            _cards.Add(card);
        }

        public void Add(CardDeck deck) {
            foreach (DeckItem item in deck)
                for (int i = 0; i < item.Count; i++)
                    Add(item.Card);
        }

        public Card GetCard() {
            if (_cards.Count > 0) {
                Card card = _cards[0];
                _cards.RemoveAt(0);
                return card;
            } else
                return null;
        }

        public void GetPlayerAbilities(ICollector<AbilityCard> abilities) {
            int i = 0;
            while (i < _cards.Count) {
                Card card = _cards[i];
                if (card.CardType == CardType.PlayerAbility) {
                    abilities.Add(card as AbilityCard);
                    _cards.RemoveAt(i);
                } else {
                    i++;
                }
            }
        }

        public void MoveTo(GameCardDeck destination) {
            if (destination == null || destination == this) return;
            destination._cards.AddRange(_cards);
            _cards.Clear();
        }

        public void Shuffle(Randomizer random) {
            if (_cards.Count < 2) return;
            List<Card> temp = new List<Card>(_cards.Count);
            while (_cards.Count > 0) {
                int i = random.Next(_cards.Count);
                temp.Add(_cards[i]);
                _cards.RemoveAt(i);
            }
            _cards = temp;
        }
    }
}
